package com.jzqf.harmonydemo.ability.slice;

import com.jzqf.harmonydemo.ResourceTable;
import com.jzqf.harmonydemo.base.BaseAbilitySlice;
import com.jzqf.harmonydemo.utils.LogUtil;
import com.jzqf.harmonydemo.utils.file.DownloadFile;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.app.Context;
import ohos.app.Environment;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;

public class ThreadAbilitySlice extends BaseAbilitySlice {
    private final String TAG = ThreadAbilitySlice.class.getSimpleName();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setTitle("任务分发器");
        initView();
        getPath(this);
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_thread;
    }

    private void initView() {
        Button globalBtn = (Button) findComponentById(ResourceTable.Id_global_btn);
        globalBtn.setClickedListener(component -> {
            TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            Revocable revocable = globalTaskDispatcher.asyncDispatch(runnable);
        });
        findComponentById(ResourceTable.Id_uiTask_btn).setClickedListener(component -> {
            TaskDispatcher uiTaskDispatcher = getUITaskDispatcher();
            uiTaskDispatcher.asyncDispatch(runnable);
        });
        findComponentById(ResourceTable.Id_download_btn).setClickedListener(component -> downloadFile());
    }

    private final Runnable runnable = () -> LogUtil.d(TAG, "Thread=" + Thread.currentThread().getName());

    private void downloadFile() {
        DownloadFile.downloadFile("http://soonsmart.keji.io/iodevface/2021-04-09/face-V2.6.6-2021040909.apk",
                getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath());
    }

    private void uploadFile() {
        String filePath = "";
        //TODO 上传文件
    }

    private void getPath(Context context) {
        LogUtil.d(TAG, "getCacheDir=" + context.getCacheDir().getPath());
        LogUtil.d(TAG, "getCodeCacheDir=" + context.getCodeCacheDir().getPath());
        LogUtil.d(TAG, "getDatabaseDir=" + context.getDatabaseDir().getPath());
        LogUtil.d(TAG, "getDataDir=" + context.getDataDir().getPath());
//        LogUtil.d(TAG, "getDir=" + context.getDir().getPath());
        LogUtil.d(TAG, "getExternalCacheDir=" + context.getExternalCacheDir().getPath());
        LogUtil.d(TAG, "getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)=" + context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath());
        LogUtil.d(TAG, "getDistributedDir=" + getDistributedDir().getPath());
        LogUtil.d(TAG, "getFilesDir=" + getFilesDir().getPath());
        LogUtil.d(TAG, "getNoBackupFilesDir=" + getNoBackupFilesDir().getPath());
        LogUtil.d(TAG, "getPreferencesDir=" + getPreferencesDir().getPath());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
